package com.example.wms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.classes.Admin;
import com.example.classes.DataBase;
import com.example.classes.Member;

/**
 * Activity for managing the Home screen.
 * @author      Alexander Huynh <a.huynh @ gatech.edu>
 * @version     1.1
 * @since       2013-03-05
 */
public class HomeHandler extends Activity
{
    private String username = "";

    @Override public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_screen);
        Intent intent = getIntent();

        username = intent.getStringExtra("username");
        TextView unText = (TextView)findViewById(R.id.home_greeting);
        String str = "Welcome, " + username + ".";
        unText.setText(str);

        String email = intent.getStringExtra("email");
        TextView unContact = (TextView)findViewById(R.id.home_contactField);

        DataBase db = new DataBase(this);
        db.open();
        Member m;
        try{m=db.getEntry(username);} catch(Exception e){m=null;}      //prevent sql injection
        db.close();

        unContact.setText(m.getContact());

        Log.v("WMS-DEBUG", "Checking...");
        Log.v("WMS-DEBUG", m.toString());
        if (!(m instanceof Admin))
        {
            Button adminBtn = (Button)findViewById(R.id.hm_admin);
            adminBtn.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * Called when the "Lost" button is pressed. Goes to the Lost screen.
     * @param view the content view
     */
    public void startLost(View view)
    {
        Intent addIntent = new Intent(this, AddItemActivity.class);
        addIntent.putExtra("username", username);
        addIntent.putExtra("found",false);
        startActivity(addIntent);
    }
    /**
     * Called when the "Found" button is pressed. Goes to the Found screen.
     * @param view the content view
     */
    public void startFound(View view)
    {
        Intent addIntent=new Intent(this,AddItemActivity.class);
        addIntent.putExtra("username",username);
        addIntent.putExtra("found",true);
        startActivity(addIntent);
    }

    /**
     * Called when the "Lost Items" button is pressed. Goes to the Lost Items screen.
     * @param view the content view
     */
    public void startFoundList(View view)
    {
        Intent newIntent=new Intent(this,itemListViewActivity.class);
        newIntent.putExtra("username",username);
        newIntent.putExtra("found",true);
        startActivity(newIntent);
    }

    /**
     * Called when the "Found Items" button is pressed. Goes to the Found Items screen.
     * @param view the content view
     */
    public void startLostList(View view)
    {
        Intent addIntent = new Intent(this, itemListViewActivity.class);
        addIntent.putExtra("username",username);
        addIntent.putExtra("found",false);
        Log.v("WMS-DEBUG", "HOME: Making a List View.");
        startActivity(addIntent);
    }

    /**
     * Called when the "Admin settings" button is pressed. Goes to the Admin screen.
     * @param view the content view
     */
    public void gotoAdmin(View view)
    {
        Intent adminIntent = new Intent(this, AdminHandler.class);
        adminIntent.putExtra("username", username);
        startActivity(adminIntent);
    }

    /**
     * Called when the "Logoff" button is pressed. Returns to the login screen.
     * @param view the content view
     */
    public void logoff(View view)
    {
        finish();
    }
}
